package com.woyou.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit.RetrofitError;
import android.content.Context;
import android.view.View;

import com.citaq.ideliver.R;
import com.woyou.bean.Goods;
import com.woyou.bean.Info;
import com.woyou.bean.Result;
import com.woyou.bean.UserInfo;
import com.woyou.bean.rpc.SendOrderReq;
import com.woyou.bean.rpc.SendOrderRes;
import com.woyou.ui.activity.AdjustOrderActivity_;
import com.woyou.ui.activity.ConfirmOrderActivity;
import com.woyou.ui.activity.ConfirmOrderActivity_;
import com.woyou.ui.activity.LoginActivity_;
import com.woyou.ui.activity.OrderStatusActivity_;
import com.woyou.ui.activity.PayOnlineActivity;
import com.woyou.ui.view.SuperUI;
import com.woyou.utils.DeviceUtils;

public class ConfirmOrderActController extends SuperController {

	private ConfirmOrderActivity mView;

	private boolean isClose = false;
	// 支付类型
	private int payType = 1;

	// 发送订单的请求bean
	private SendOrderReq orderReq = new SendOrderReq();
	
	private long lastClickTime; 

	public ConfirmOrderActController(Context context, SuperUI superUI) {
		super(context, superUI);
		mView = (ConfirmOrderActivity) superUI;
	}

	/** 
	* 防止重复点击 
	* @return 
	*/ 
	public boolean isFastDoubleClick() {
		long time = System.currentTimeMillis();
		long timeD = time - lastClickTime;
		if (0 < timeD && timeD < 1500) {
			return true;
		}
		lastClickTime = time;
		return false;
	}
	
	@Override
	public boolean backOperate() {
		// TODO Auto-generated method stub
		return false;
	}

	/**发送订单信息*/
	public void sendOrder() {
		executeTask(new Runnable() {

			@Override
			public void run() {
				// 显示进度条
				showProgressDialog("订单正在提交中，请稍等...", true);
				// 用户信息
				UserInfo userInfo = mUserModel.getUserInfo(); // 获取用户信息

				orderReq.setsId(mView.shopItem.getsId());
				orderReq.setuId(userInfo.getuId());
				orderReq.setPwd(userInfo.getPwd());
				orderReq.setContactor(mView.addr.getContact());
				orderReq.setPhone(mView.addr.getPhone());
				orderReq.setSex(mView.addr.getSex());
				orderReq.setAddrId(mView.addr.getAddrId());
				orderReq.setAddrName(mView.addr.getAddrName());
				// 修改付款类型
				orderReq.setPayType(payType);
				if (mView.onlineLayout.getVisibility() == View.GONE) {
					payType = 2;
					orderReq.setPayType(payType);
				}
				// 阶梯价ID
				orderReq.setModeId(mView.shopDetail.getModeId());
				String pushToken = DeviceUtils.getPushToken(mContext.getApplicationContext());
				orderReq.setToken(pushToken); // 推送消息的token
				if (mView.deliverTime != null) {
					orderReq.setTmId(mView.deliverTime.getId() + ""); // 时间段编号
					orderReq.setOrderDate(mView.deliverTime.getDate()); // 下单日期
				} else {
					dismissProgressDialog();
					showToastOnUI("请选择外送时间");
					return;
				}
				orderReq.setRemarks("" + mView.remarkTx.getText().toString()); // 备注
				orderReq.setMealsBoxSum(mShoppingCarModel.getMealsBoxFee());// 餐盒费
				orderReq.setStamp(mView.currentTime + ""); // 时间戳
				orderReq.setGoodsList(mShoppingCarModel.getOrderGoodsItemList()); // 购买的商品列表
				orderReq.setCouponList(mShoppingCarModel.getReqCouponsList()); // 选择的优惠券列表
				orderReq.setActyList(mActivitiesModel.getOrderGiftList());// 选择的赠品列表

				try {
					closeBtn();
					// 发送订单
					final Result<SendOrderRes> result = mOrderModel.v2_3sendOrder(orderReq);
					runOnUI(new Runnable() {
						@Override
						public void run() {
							openBtn();
							if (result != null && 1 == result.code) {
								Map<String, Object> map = new HashMap<String, Object>();
								map.put("req", orderReq);
								map.put("orderid", result.getData().getOrderNo());
								map.put("phone", mView.shopDetail.getPhone());
								map.put("name", mView.shopDetail.getsName());
								map.put("time", 30 * 60);
								map.put("price", mShoppingCarModel.getTotalCost());
								Info<Map<String, Object>> info = new Info<Map<String, Object>>(map);
								info.setOrigin(ConfirmOrderActivity_.class);
								if (orderReq.getPayType() == 1) {
									openActivity(PayOnlineActivity.class, info);
								} else {
									openActivity(OrderStatusActivity_.class, info);
								}

							} else if (result != null && -2 == result.code && result.getData() != null) {
								Info<List<Goods>> info = new Info<List<Goods>>(result.getData().getGoodsList());
								openActivity(AdjustOrderActivity_.class, info);

							} else if (result != null && -3 == result.code) {
								showToast(result.getMsg());
								openActivity(LoginActivity_.class, null);

							} else {
								showToast(result.msg);
							}
							dismissProgressDialog();
						}

					});
				} catch (RetrofitError e) {
					switch (e.getKind()) {
					case HTTP:
						showToastOnUI("网络不给力，请检查您的网络!");
						break;
					case CONVERSION:
						showToastOnUI("很抱歉,服务器又任性!");
						break;
					case NETWORK:
						showToastOnUI("网络不给力，请检查您的网络!");
						break;
					case UNEXPECTED:
						showToastOnUI("很抱歉,服务器又任性!");
						break;
					}
					dismissProgressDialog();
				}
			}
		});

	}

	public void clickView(View v) {
		switch (v.getId()) {
		case R.id.pay_online:
			// 在线支付
			mView.onlinePayImage.setImageResource(R.raw.rediobox1);
			mView.deliverPayImage.setImageResource(R.raw.rediobox2);
			// 修改付款类型
			payType = 1;
			break;

		case R.id.pay_deliver:
			// 货到付款
			mView.onlinePayImage.setImageResource(R.raw.rediobox2);
			mView.deliverPayImage.setImageResource(R.raw.rediobox1);
			// 修改付款类型
			payType = 2;
			break;

		default:
			break;
		}
	}

	public void closeBtn() {
		isClose = true;
	}

	public void openBtn() {
		isClose = false;
	}

	public boolean isClose() {
		return isClose;
	}

}
